package com.itheima.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author rosun
 * TODO: 报表system
 */
@RestController   //  @Controller +@ResponseBody
@Api(tags = "报表")
public class POIController {


    @Reference
    private MemberService memberService;

    @Reference
    SetmealService setmealService;

    /**
     * TODO:无条件查询会员增长
     * @return
     */
   @PostMapping("findmember")
   public Result findmember(){
       try {
               HashMap map = memberService.findmember();
               return new Result(true, "无条件会员数据查询成功", map);
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,"会员数据查询异常");
       }
   }



    /**
     * TODO:有条件查询会员增长
     * @param dateList 日期范围
     * @return
     * @throws Exception DateUtils转换异常
     */
   @PostMapping("findmemberbydate")
   public Result findmemberbydate(@RequestBody List<Date> dateList) throws Exception {
       try {
           long timeBegin = dateList.get(0).getTime();
           long timeEnd = dateList.get(1).getTime();
           if (timeBegin > timeEnd) {
               return new Result(false, "日子不会过了领导？左边小右边大！！");
           } else {
               String begin = DateUtils.parseDate2String(dateList.get(0),"yyyy-MM-dd");
               String  end =  DateUtils.parseDate2String(dateList.get(1),"yyyy-MM-dd");
               HashMap map = memberService.findMemberByDate(begin, end);
               return new Result(true, "你轻轻一点的成功，是我无数脑油的迸发",map);
           }
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,"出现了奇奇怪怪的错误，这个代码不是朱鸿锦写的");
       }
   }

   @PostMapping("findSetmealPOI")
    public Result findSetmealPOI(){
       try {
           List<HashMap> sexDate = memberService.findmemberBySex();
           List<HashMap> ageDate = memberService.findmemberByAge();
           HashMap<String, List<HashMap>> map = new HashMap<>();
           map.put("sexDate", sexDate);
           map.put("ageDate", ageDate);
          return new Result(true,"分类查询会员成功",map);
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,"分类查询会员失败");

       }

   }

    //  套餐预约占比
    @GetMapping("getSetmealZhanBi")
    public Result getSetmealZhanBi(){
        try {
            List<Map> map =  setmealService.getSetmealReport();
            return  new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }
}
